home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 4 #4 / IMG 36 April 1996.iso / Essentials / MGD 4⁄96ƒ / HelloWorld6.c next >
Text File  |  1995-06-29  |  24KB  |  520 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        HelloWorld6.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun            \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Demo program for Macintosh GameWriter 1.0.0, a training program…                        \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                            \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20. // This program opens a window, displays an 8-bit color background, and at first…
  21. // …mouseclick, runs two clams across the screen. At second mouseclick, one clam stops,…
  22. // …talks, and flaps its/his/her face for 40 frames as the other clam continues running.
  23. // When the other clam runs up to the standing clam, there is an impact.
  24.  
  25. // If the monitor is not set to 8-bit, a soft alert is displayed, asking if the user…
  26. // …would like to switch to 8-bit, or stick with the current setting.
  27.  
  28.  
  29. #include "MGWExterns1.h"
  30.  
  31. #define     kPutInFront    (WindowPtr)-1L
  32. #define     kWaitTicks     4L        // Sets the delay in Ticks between frames. Try 3. Try 2. Try 0.
  33. #define     kJaneStepLength    12    // Sets the distance in pixels between Clamity Jane's moves.
  34. #define     kClemStepLength    16    // Sets the distance in pixels between Clem the Clam's moves.
  35. #define  kFrontFace            0    // Enumerates the various faces of the clam sprites.
  36. #define  kBlinkFace         1
  37. #define  kEehFace            2
  38. #define  kOohFace            3
  39. #define  kStepRightFace        4
  40. #define  kWalkRightFace        5
  41. #define  kRunRightFace        6
  42. #define  kDizzyRightFace1    7
  43. #define  kDizzyLeftFace1    8
  44. #define  kDizzyRightFace2    9
  45. #define  kDizzyLeftFace2    10
  46. #define  kMaxFaces            11
  47. #define  kBounce            -2    // Sets how many steps the running clam bounces back on impact.
  48. #define  kLastFace        40        // Sets how many frames the clams stay visible after impact.
  49.         
  50.                 // The resource constants--with 'r' prefix like Apple wants them.
  51. #define  rJaneFacesID    136        // The 'PICT' ID# where the views of Jane are located.
  52. #define  rClemFacesID    135        // The 'PICT' ID# where the views of Clem are located.
  53. #define  rMasksID        130        // The 'PICT' ID# where the clam masks are located.
  54. #define  rBackgroundID    134        // The 'PICT' ID# where the background picture is located.
  55. #define  rMainWindowID    128        // The 'WIND' resource ID# for the main window.
  56.  
  57. #define  rHelloSndID    3000
  58. #define  rFootstepSndID    3001
  59. #define  rImpactSndID    3002
  60. #define  rDizzySndID    3003
  61.  
  62. #define  kColorBitsPreferred    8
  63.     
  64. typedef struct
  65. {
  66.     Rect    face;
  67.     Rect    mask;
  68. } tSpriteType;
  69.  
  70.  
  71. Rect    bigPictureRect, masksRect,  allComboRect;
  72. Rect    janeFacesRect, janeIsAtRect, janeWasAtRect, janeComboRect;
  73. Rect    clemFacesRect, clemIsAtRect, clemWasAtRect, clemComboRect;
  74. CGrafPtr    workCPort, janeFacesCPort, clemFacesCPort, backgroundCPort;
  75. GrafPtr mainWindow, masksPort;
  76. Boolean    itWorked, contactFlag, impactReadyFlag, gameOverFlag, evenFrame;    // Note new flags.
  77. long    targetTick;
  78. short    janeSprite, clemSprite, thisFaceCounter;
  79.  
  80. tSpriteType    sprite[kMaxFaces];
  81.  
  82. extern    Boolean        gUserWantsSound;
  83.     
  84.     
  85. //==============================================================  Prototypes
  86.  
  87. void InitAll(void);
  88. void OpenMainWindow (void);
  89. void SetTheRects(void);
  90. void SetTheCPorts(void);
  91. void CopyBothAtOnce (void);
  92. void CopyOneAtATime (void);
  93. void ShowClams (void);
  94. void DoDelay (void);
  95. void JaneLoop (void);
  96. void ClemLoop (void);
  97. void JaneSpeaks (void);
  98. void DoImpact (void);
  99.  
  100. //==============================================================  Functions
  101.  
  102. //--------------------------------------------------------------  InitAll
  103.  
  104. void InitAll(void)
  105. {
  106.     InitToolbox();
  107.     if (WhatsOurDepth() < kColorBitsPreferred)    // Compare color depth with what we want.
  108.         YellowAlert(kPref8BitColor);            // If smaller, notify user.
  109.     if (WhatsOurDepth() > kColorBitsPreferred)    // Compare color depth with what we want.
  110.         YellowAlert(kPrefDownTo8BitColor);        // If larger, notify user.
  111.     gUserWantsSound = TRUE;
  112.     InitializeForSound();
  113.     SetTheRects();        // Since some of these Rects define fields in CGrafPorts,…
  114.     SetTheCPorts();        // …set the rects before setting the ports.
  115.     janeSprite = kStepRightFace;
  116.     clemSprite = kRunRightFace;
  117.     thisFaceCounter = 0;
  118.     contactFlag = FALSE;        // Not touching,
  119.     impactReadyFlag = FALSE;    // Not about to collide,
  120.     gameOverFlag = FALSE;        // Not done playing.
  121.     evenFrame = TRUE;
  122.     targetTick = TickCount() + kWaitTicks;
  123.     HideCursor();        // This demo doesn't use mouse input, so why look at it?
  124. }
  125.  
  126. //--------------------------------------------------------------  OpenMainWindow
  127.  
  128. void OpenMainWindow (void)
  129. {
  130.     mainWindow = GetNewCWindow(128, 0L, kPutInFront);    // Load window from resource.
  131.     ShowWindow((GrafPtr)mainWindow);                    // Now display it.
  132.     SetPort((GrafPtr)mainWindow);                        // Make its port current.
  133.     ClipRect(&bigPictureRect);                            // Set its clip region.
  134.     CopyRgn(mainWindow->clipRgn, mainWindow->visRgn);    // Set its visRgn.
  135.     ForeColor(blackColor);                                // Set its pen color to black.
  136.     BackColor(whiteColor);                                // Set background color white.
  137. }
  138.  
  139. //--------------------------------------------------------------  SetTheRects
  140.  
  141. void SetTheRects(void)    // The most tedious part of programming this type of game.
  142. {
  143.     SetRect(&janeFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  144.     SetRect(&clemFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  145.     SetRect(&masksRect, 0, 0, 448, 32);        // Size and shape of BitMap for the sprite masks.
  146.     SetRect(&bigPictureRect, 0, 0, 512, 322);    // The shape of the picture we'll put in the main window, workCPort and backgroundCPort.}
  147.     SetRect(&janeIsAtRect, 80, 240, 112, 272);    // The shape (32 x 32) of the images of Jane, and the position of the first image.}
  148.     janeWasAtRect = janeIsAtRect;                // Initializing janeIsAtRect...it has to start somewhere, and this is handy.}
  149.     janeComboRect = janeIsAtRect;
  150.     SetRect(&clemIsAtRect, 40, 244, 72, 276);    // The shape (32 x 32) of the images of Clem, and the position of the first image.}
  151.     clemWasAtRect = clemIsAtRect;                // Initializing clemIsAtRect...it has to start somewhere, and this is handy.}
  152.     clemComboRect = clemIsAtRect;
  153.         // And now, the tedium. In this sample, all we're doing is showing the clam running across the screen to the right.}
  154.         // However, if you use ResEdit and look at 'PICT' 129 in Sample.rsrc, you'll find 28 different views of the clam.}
  155.         // If we wanted the clam to run left too, and walk slowly, and face the user, and blink its eyes, we'd be calling…}
  156.         // …SetRect 56 times--one face and one mask per sprite. And if we had jumping clams and rear views of clams…}
  157.         // …and starfish and clamdiggers and other hazards of the clam environment, we might have hundreds of rects to set.}
  158.     SetRect(&sprite[kFrontFace].face, 320, 32, 352, 64);    // The shape and position of sprite[kFrontFace].face on facesCPort.portPixMap.
  159.     SetRect(&sprite[kFrontFace].mask, 320, 0, 352, 32);    // The shape and position of sprite[kFrontFace].mask on masksPort.portPixMap.
  160.     SetRect(&sprite[kBlinkFace].face, 320, 0, 352, 32);    // Note that some faces (e.g. eyes open or closed) use the same mask,…
  161.     SetRect(&sprite[kBlinkFace].mask, 320, 0, 352, 32);    // …since they have the same silhouette.}
  162.     SetRect(&sprite[kEehFace].face, 352, 0, 384, 32);    // I could write more comments here, but setting these rects…
  163.     SetRect(&sprite[kEehFace].mask, 352, 0, 384, 32);    // …is already tedious enough without a bunch of busy-work.
  164.     SetRect(&sprite[kOohFace].face, 352, 32, 384, 64);
  165.     SetRect(&sprite[kOohFace].mask, 352, 0, 384, 32);    
  166.     SetRect(&sprite[kStepRightFace].face, 192, 0, 224, 32);    
  167.     SetRect(&sprite[kStepRightFace].mask, 192, 0, 224, 32);    
  168.     SetRect(&sprite[kWalkRightFace].face, 224, 0, 256, 32);    
  169.     SetRect(&sprite[kWalkRightFace].mask, 224, 0, 256, 32);    
  170.     SetRect(&sprite[kRunRightFace].face, 160, 0, 192, 32);    // BTW, there are plenty more clam faces and masks in the 'PICT's,…
  171.     SetRect(&sprite[kRunRightFace].mask, 160, 0, 192, 32);    // …if you feel you need rect setting practice.  :-)
  172.     SetRect(&sprite[kDizzyRightFace1].face, 384, 0, 416, 32);        // Hey look! Here's more now!
  173.     SetRect(&sprite[kDizzyRightFace1].mask, 384, 0, 416, 32);    
  174.     SetRect(&sprite[kDizzyRightFace2].face, 384, 32, 416, 64);    
  175.     SetRect(&sprite[kDizzyRightFace2].mask, 384, 0, 416, 32);    
  176.     SetRect(&sprite[kDizzyLeftFace1].face, 416, 0, 448, 32);    
  177.     SetRect(&sprite[kDizzyLeftFace1].mask, 416, 0, 448, 32);    
  178.     SetRect(&sprite[kDizzyLeftFace2].face, 416, 32, 448, 64);    
  179.     SetRect(&sprite[kDizzyLeftFace2].mask, 416, 0, 448, 32);    
  180.     
  181. }
  182.  
  183. //--------------------------------------------------------------  SetTheCPorts
  184.  
  185. void SetTheCPorts(void)    // Create the CGrafPorts and load their .portPixMap fields.
  186. {
  187.             // Create BitMap for sprite masks. NOTE THIS IS A BITMAP!
  188.     CreateOffScreenBitMap (&masksRect, &masksPort);
  189.     LoadGraphic (rMasksID);        // …load 'PICT' resource for the clam masks.
  190.     
  191.             // Create PixMap for Jane faces.
  192.     CreateOffScreenPixMap (&janeFacesRect, &janeFacesCPort);
  193.     LoadGraphic (rJaneFacesID);        // …load 'PICT' resource for the clam faces.
  194.  
  195.             // Create PixMap for Clem faces.
  196.     CreateOffScreenPixMap (&clemFacesRect, &clemFacesCPort);
  197.     LoadGraphic (rClemFacesID);        // …load 'PICT' resource for the clam faces.
  198.  
  199.             // Create PixMap for the background.
  200.     CreateOffScreenPixMap (&bigPictureRect, &backgroundCPort);
  201.     LoadGraphic(rBackgroundID);    // …load 'PICT' resource for the background picture.
  202.     
  203.             // Create PixMap for offscreen graphics work.
  204.     CreateOffScreenPixMap (&bigPictureRect, &workCPort);
  205.  
  206.     OpenMainWindow();
  207.  
  208.         //{This fills the main window with the background picture, so the user can see it.
  209.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  210.         &((GrafPtr)mainWindow)->portBits, 
  211.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  212.  
  213. // This fills the workCPort.portPixMap with the background picture, so updates can be done quickly.
  214.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  215.         &((GrafPtr)workCPort)->portBits, 
  216.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  217. }
  218.  
  219. //--------------------------------------------------------------  CopyBothAtOnce
  220.  
  221. void CopyBothAtOnce (void)    //Display the clams on the screen when they're close together.
  222. {
  223.         //UnionRect(clemComboRect, larryComboRect, allComboRect);
  224.         //{Find the smallest rectangle which will cover Larry and Clem.}
  225.     UnionRect(&janeComboRect, &clemComboRect, &allComboRect);
  226. // Find the smallest rectangle which will cover the old position of Clem and the new.
  227.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  228.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  229.         &(((GrafPtr)mainWindow)->portBits), 
  230.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  231.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  232.         //{Restore the workPort by covering up our clams with the background they obscure.  This way, workPort is…}
  233.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  234.         &(((GrafPtr)workCPort)->portBits), 
  235.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  236. // Restore the workCPort by covering up our clam with the background it obscures.
  237. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  238. // without having to copy the entire PixMap.
  239. }
  240.  
  241. //--------------------------------------------------------------  CopyOneAtATime
  242.  
  243. void CopyOneAtATime (void)    // Display the clams on the screen when they're far apart.
  244. {
  245.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, larryComboRect, larryComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  246.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  247.         &(((GrafPtr)mainWindow)->portBits), 
  248.         &janeComboRect, &janeComboRect, srcCopy, mainWindow->visRgn);
  249. // Copy the contents of janeComboRect from workCPort->portPixMap to the main window. In one swell foop, old Jane…
  250. //…will be erased, and the new Jane overlayed onto the background picture. Wallah! Flicker-free animation!
  251.     //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, clemComboRect, clemComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  252.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  253.         &(((GrafPtr)mainWindow)->portBits), 
  254.         &clemComboRect, &clemComboRect, srcCopy, mainWindow->visRgn);
  255. // Copy the contents of clemComboRect from workCPort->portPixMap to the main window. In one swell foop, old Clem
  256. //…will be erased, and the new Clem overlayed onto the background picture. Wallah! Flicker-free animation!
  257.     //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, larryRect, larryRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  258.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  259.         &(((GrafPtr)workCPort)->portBits), 
  260.         &janeIsAtRect, &janeIsAtRect, srcCopy, mainWindow->visRgn);
  261. // Restore the workCPort by covering up Jane's image with the background it obscures.
  262. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  263. // without having to copy the entire PixMap.
  264.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  265.         &(((GrafPtr)workCPort)->portBits), 
  266.         &clemIsAtRect, &clemIsAtRect, srcCopy, mainWindow->visRgn);
  267. // Restore the workCPort by covering up Clem's image with the background it obscures.
  268. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  269. // without having to copy the entire PixMap.
  270.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, clemRect, clemRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  271. }
  272.  
  273. //--------------------------------------------------------------  ShowClam 
  274.  
  275. void ShowClams (void)    // Do the animation and make it appear on the screen.
  276. {
  277.     Rect dummyRect;
  278.     
  279.     CopyMask(&((GrafPtr)janeFacesCPort)->portBits, 
  280.         &((GrafPtr)masksPort)->portBits, 
  281.         &((GrafPtr)workCPort)->portBits, 
  282.         &sprite[janeSprite].face, 
  283.         &sprite[janeSprite].mask, 
  284.         &janeIsAtRect);
  285. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  286. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  287. // previous image of the clam visible on the screen while we worked.
  288.  
  289.     UnionRect(&janeWasAtRect, &janeIsAtRect, &janeComboRect);
  290. // Find the smallest rectangle which will cover the old position of Jane and the new.
  291.  
  292. CopyMask(&((GrafPtr)clemFacesCPort)->portBits, 
  293.         &((GrafPtr)masksPort)->portBits, 
  294.         &((GrafPtr)workCPort)->portBits, 
  295.         &sprite[clemSprite].face, 
  296.         &sprite[clemSprite].mask, 
  297.         &clemIsAtRect);
  298. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  299. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  300. // previous image of the clam visible on the screen while we worked.
  301.  
  302.     UnionRect(&clemWasAtRect, &clemIsAtRect, &clemComboRect);
  303. // Find the smallest rectangle which will cover the old position of Clem and the new.
  304.  
  305.  
  306.     if (SectRect(&clemIsAtRect, &janeIsAtRect, &dummyRect))
  307.     {
  308.         contactFlag = TRUE;
  309.         CopyBothAtOnce();
  310.     }
  311.     else
  312.     {
  313.         contactFlag = FALSE;
  314.         CopyOneAtATime();
  315.     }
  316. }
  317. //    CopyBits(&((GrafPtr)workCPort)->portBits, 
  318.         //&(((GrafPtr)mainWindow)->portBits), 
  319.         //&clamComboRect, &clamComboRect, srcCopy, mainWindow->visRgn);
  320. // Copy the contents of comboRect from workCPort->portPixMap to the main window. In one swell foop, the old clam…}
  321. //…will be erased, and the new clam overlayed onto the background picture. Wallah! Flicker-free animation!}
  322.  
  323.     //CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  324.         //&(((GrafPtr)workCPort)->portBits), 
  325.         //&clamIsAtRect, &clamIsAtRect, srcCopy, mainWindow->visRgn);
  326. // Restore the workCPort by covering up our clam with the background it obscures.
  327. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  328. // without having to copy the entire PixMap.
  329. //}
  330.  
  331. //--------------------------------------------------------------  DoDelay
  332.  
  333. // This is the companion function to the above function (LogNextTick()).
  334. // We do nothing but loop until TickCount() catches up with (or passes) our…
  335. // global variable tickNext.
  336.  
  337. void DoDelay (void)
  338. {
  339.     do
  340.     {
  341.     }
  342.     while (TickCount() < targetTick);            // Loop until TickCount() catches up.
  343.     targetTick = TickCount() + kWaitTicks;
  344. }
  345.  
  346. //--------------------------------------------------------------  JaneLoop
  347.  
  348. void JaneLoop (void)
  349. {
  350.     switch    (janeSprite)                    //If the current view of jane is…
  351.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  352.         {    janeSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  353.             break;    }
  354.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  355.         {    janeSprite = kRunRightFace;        // …kRunRightFace.
  356.             break;    }
  357.     // And if it was neither kStepRightFace nor kWalkRightFace, then janeSprite was either…
  358.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  359.             janeSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  360.     }
  361.     
  362.     janeWasAtRect = janeIsAtRect;            // Store the clam's current position as its last position,…
  363.                                             // …we'll be erasing it next time through the loop.
  364.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position--it'll be…
  365.                                             // …kStepLength pixels to the right of its last position.
  366.     if (janeIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  367.     {                                        // …set the right border of clamIsAtRect…
  368.         janeIsAtRect.right = 0;                // …to the left edge of the screen…
  369.         janeIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  370.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  371. }
  372.  
  373. //--------------------------------------------------------------  ClemLoop
  374.  
  375. void ClemLoop (void)
  376. {
  377.     switch    (clemSprite)                    //If the current view of Clem is…
  378.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  379.         {    clemSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  380.             break;    }
  381.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  382.         {    clemSprite = kRunRightFace;        // …kRunRightFace.
  383.             break;    }
  384.     // And if it was neither kStepRightFace nor kWalkRightFace, then clemSprite was either…
  385.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  386.             clemSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  387.     }
  388.     clemWasAtRect = clemIsAtRect;            // Store the clam's current position as its last position,…
  389.                                             // …we'll be erasing it next time through the loop.
  390.     OffsetRect(&clemIsAtRect, kClemStepLength, 0);    // Set the clam's next position--it'll be…
  391.                                             // …kStepLength pixels to the right of its last position.
  392.     if (clemIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  393.     {                                        // …set the right border of clamIsAtRect…
  394.         clemIsAtRect.right = 0;                // …to the left edge of the screen…
  395.         clemIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  396.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  397.     if (clemSprite == kStepRightFace)                // Only sound a footstep in pose where foot strikes ground.
  398.           PlayASound(rFootstepSndID, kLowSoundPriority);        //Ahh, the pitter patter of tiny feet.
  399. }
  400.  
  401. //--------------------------------------------------------------  JaneSpeaks
  402.  
  403. // This routine has the clam stop moving, face the screen, and move its face.
  404.  
  405. void JaneSpeaks (void)
  406. {    Rect dummyRect;
  407.     if ((impactReadyFlag) && (SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))
  408.         DoImpact();
  409.     thisFaceCounter = thisFaceCounter + 1;
  410.          switch (thisFaceCounter)
  411.          {    case    (1): 
  412.             {    janeSprite = kFrontFace;
  413.                 break;    }
  414.             case    (2): 
  415.             {    janeSprite = kEehFace;
  416.                 break;    }
  417.             case    (3):     // This keeps an impact from occurring when both clams are in the same…
  418.             {    if (!(SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))    // …place when the…
  419.                     impactReadyFlag = TRUE;                                    // …button is pushed.
  420.                 break;    }
  421.             case    (4): 
  422.             {    janeSprite = kFrontFace;
  423.                 break;    }
  424.             case    (5): 
  425.             {    janeSprite = kOohFace;
  426.                 break;    }
  427.             case    (9): 
  428.             {    janeSprite = kEehFace;
  429.                 break;    }
  430.             case    (11): 
  431.             {    janeSprite = kOohFace;
  432.                 break;    }
  433.             case    (13): 
  434.             {    janeSprite = kFrontFace;
  435.                 break;    }
  436.             case    (16): 
  437.             {    janeSprite = kBlinkFace;
  438.                 break;    }
  439.             case    (18): 
  440.             {    janeSprite = kFrontFace;
  441.                 break;    }
  442.             case    (35): 
  443.             {    janeSprite = kBlinkFace;
  444.                 break;    }
  445.             case    (38): 
  446.             {    janeSprite = kFrontFace;
  447.                 break;    }
  448.             case    (39): 
  449.             {    impactReadyFlag = TRUE;
  450.                 break;    }
  451.         }
  452. }
  453.  
  454. //--------------------------------------------------------------  DoImpact
  455.  
  456. void DoImpact (void)
  457. {
  458.     janeWasAtRect = janeIsAtRect;    // Store the clam's current position as its last position.
  459.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position.
  460.     clemWasAtRect = clemIsAtRect;        // Store the clam's current position as its last position.
  461.     OffsetRect(&clemIsAtRect, kClemStepLength * kBounce, 0);    // Set the clam's next position.
  462.     PlayASound(rImpactSndID, kHighestSoundPriority);        // High enough to interrupt anything.
  463.     thisFaceCounter = 0;
  464.     while (thisFaceCounter <= kLastFace)
  465.     {
  466.         ShowClams();
  467.         DoDelay();                // Do nothing for a while.
  468.         PlayLoopSound(rDizzySndID, kHighSoundPriority);    // Not high enough to interrupt the impact.
  469.         thisFaceCounter = thisFaceCounter + 1;
  470.         evenFrame = !evenFrame;
  471.         if (evenFrame)
  472.         {
  473.             janeSprite = kDizzyLeftFace1;
  474.             clemSprite = kDizzyRightFace1;
  475.         }
  476.         else
  477.         {
  478.             janeSprite = kDizzyLeftFace2;
  479.             clemSprite = kDizzyRightFace2;
  480.         }
  481.     }
  482.     gameOverFlag = TRUE;        // And we're outta here. Game over.
  483. }
  484.  
  485. //--------------------------------------------------------------  main
  486. //----------------------------------------------------------------------
  487.  
  488. void main(void)
  489. {
  490.     InitAll();
  491.     while (!Button())    // Before the user presses the mouse button, nothing happens.
  492.         {
  493.         }
  494.     while (Button())    // When the user presses the mouse button, nothing happens.
  495.         {
  496.         }
  497.     while (!Button())    // When the mouse button is released, continue to…
  498.         {
  499.         ShowClams();    // Put the clams on the screen.
  500.         DoDelay();        // Keep everything from happening too fast.
  501.         JaneLoop();        // Put Jane in her next position.
  502.         ClemLoop();        // Put Clem in his next position.
  503.         }
  504.     PlayASound(rHelloSndID, kMediumSoundPriority);    // Sound, "Hello" when button is pushed.
  505.     while (!(gameOverFlag))    // …until the clams crash into each other and fall down dizzy.
  506.         {    
  507.         ShowClams();    // Put the clams on the screen.
  508.         DoDelay();        // Keep everything from happening too fast.
  509.         JaneSpeaks();    // Make mouth motions, shift into impact mode when ready.
  510.         ClemLoop();        // Put Clem in his next position.
  511.         }
  512.     CloseDownSound();
  513.     InitCursor();    // Rarely needed, since most programs call InitCursor on startup. Still, it's…
  514.                     // a good habit to leave everything in normal condition when your programs quit.
  515. }                    // And we're done.
  516.  
  517. //------------------------------------------------------------------------------------------\\
  518. //                                    End HelloWorld6.c                                        \\
  519. //------------------------------------------------------------------------------------------\\
  520.